package com.aijk.web.admin.utils;

import com.aijk.commons.lang.utils.StringUtil;
import com.aijk.commons.utils.Utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lenovo on 2016/11/2.
 */
public class DataTableUtil {
    public  static Map FormatOptions(Map skey){
        if (skey != null && skey.containsKey("order[0][column]") && Integer.parseInt(skey.get("order[0][column]").toString()) >= 0) {
            if(Utils.isEmpty(skey.get("columns[" + skey.get("order[0][column]").toString() + "][name]")))
                skey.put("orderbyclause", skey.get("columns[" + skey.get("order[0][column]").toString() + "][data]") + " " + skey.get("order[0][dir]"));
            else
                skey.put("orderbyclause", skey.get("columns[" + skey.get("order[0][column]").toString() + "][name]") + " " + skey.get("order[0][dir]"));
        }
        if(skey.containsKey("start")&&Utils.toInteger(skey.get("start")).intValue()==0){
            skey.put("start",1);
        }else{
            skey.put("start",Utils.toInteger(skey.get("start")).intValue()/Utils.toInteger(skey.get("length")).intValue()+1);
        }
        return skey;
    }

    public static <T> T fillQo(HashMap skey, Class<T> clazz)  {
        try {
            Object bean = clazz.newInstance();
            Field[] field = clazz.getDeclaredFields();//获取基类的属性
            Method[] methods = clazz.getSuperclass().getMethods();//获取父类方法
            for (int i = 0; i < field.length; i++){
                String type = field[i].getType().toString();//得到此属性的类型
                field[i].setAccessible(true);
               /* if(field[i].getName()=="pageNo"&& Utils.isNotEmpty(skey.get("start"))){
                    fillField(bean,type,field[i],skey.get("start"));
                }else if(field[i].getName() == "pageSize" && Utils.isNotEmpty(skey.get("length"))){
                    fillField(bean,type,field[i],skey.get("length"));
                }else if(){

                }*/
                if (Utils.isNotEmpty(field[i].getName())&&Utils.hasKeyNotEmpty(skey,field[i].getName())){
                    fillField(bean,type,field[i],skey.get(field[i].getName()));
                }
            }

            for (int i = 0; i < methods.length; i++){
                Method method = methods[i];//得到此属性的类型
                try {
                    if(method.getName().equals("setPageNo")&& Utils.isNotEmpty(skey.get("start"))){
                            method.invoke(bean,Utils.toLong(skey.get("start")));
                    }else if(method.getName().equals("setPageSize")&& Utils.isNotEmpty(skey.get("length"))){
                        method.invoke(bean,Utils.toLong(skey.get("length")));
                    }else if(method.getName().equals("setSortway")&& Utils.isNotEmpty(skey.get("orderbyclause"))){
                        method.invoke(bean,Utils.toString(StringUtil.toUnderlineCase(skey.get("orderbyclause").toString())));
                    }
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
            return (T)bean;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return  null;
    }

    private static void fillField(Object bean, String type, Field field, Object val) throws IllegalAccessException {
        if (type.endsWith("string")||type.endsWith("String")) {
            field.set(bean,Utils.toString(val)) ;
        }else if(type.endsWith("int") || type.endsWith("Integer")){
            field.set(bean,Utils.toInteger(val)) ;
        }else if(type.endsWith("long") || type.endsWith("Long")){
            field.set(bean,Utils.toLong(val)) ;
        }else if(type.endsWith("short") || type.endsWith("Short")){
            field.set(bean,Utils.toShort(val)) ;
        }else if(type.endsWith("Date")){
            field.set(bean,Utils.toDate(val)) ;
        }else if(type.endsWith("boolean")||type.endsWith("Boolean")){
            field.set(bean,Utils.toBoolean(val)) ;
        }else if(type.endsWith("byte")||type.endsWith("Byte")){
            field.set(bean,Byte.parseByte(Utils.toString(val))) ;
        }
    }
}
